package com.chen.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.service.IClassifyService;
import com.chen.entity.Classify;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-23
 */
@Api(tags = "学堂分类管理")
@RestController
@RequestMapping("/classify")
public class  ClassifyController {

    @Resource
    private IClassifyService classifyService;

    @ApiOperation("添加分类或修改")
    @PostMapping
    public Result save(@RequestBody Classify classify) {
        classifyService.saveOrUpdate(classify);
        return Result.success();
    }

    @ApiOperation("删除多条分类")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        classifyService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("查询所有分类")
    @GetMapping
    public Result findAll() {
        return Result.success(classifyService.list());
    }


    @ApiOperation("分页显示所有分类")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "")  String name) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("mid");
        queryWrapper.like("name",name);
        return Result.success(classifyService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

